<div class="page">
	<div class="menu" style="margin-top:-50px;">
		<?php include('modules/members/dashboard.php');?>
    </div>
	<div class="products" style="margin-left: 40px;">
	  <form action="?m=stickers&p=upfile&upload=true" method="post" enctype="multipart/form-data">
		<div align="center">
        	<label>ตั้งชื่อไฟล์: <input type="text" name="sticker_name" id="sticker_name" style="height:16px;margin-right:40px;width:312px;"/></label> <br/>
			<label>อัพโหลดไฟล์: <input type="file" name="products_file" id="products_file" size="50" align="middle"/></label>
			<div class="clear" style="margin-top:20px;"></div>
			<input type="submit" value="เพิ่มไฟล์" />
		</div>
	  </form>
	  <?php
		if(isset($_GET['upload']) && $_GET['upload'] == "true"){
			$filename = ($_POST['sticker_name'] =="") ? $_FILES["products_file"]["name"] : $_POST['sticker_name'];
			if(copy($_FILES["products_file"]["tmp_name"],'uploads/'.$_FILES["products_file"]["name"])){
				mysql_query('INSERT INTO stickers(file_name,file_path) VALUES("'.$_POST["sticker_name"].'","uploads/'.$_FILES["products_file"]["name"].'")')or die(mysql_error());
				echo '<div class="success" style="color:green;font-size:16px;text-align:center;">Upload file complete!!</div>';
			}else{
				echo '<div class="error" style="color:red;font-size:16px;text-align:center;">Cannot Upload file!!</div>';
			}
		}
	  ?>
		<div class="clear" style="margin: 20px;"></div>
		<p style="float:right;" id="button">
        	<a href="#" class="btn" style="font-size:25px;" id="delete_now"><span>ลบที่เลือก</span></a>
       	</p>
		<table id="products_list" width="100%">
        	<thead>
              <tr>
              	<th width="50">
                <input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);">
               	</th>
            	<th width="50">ID</th>
                <th>ชื่อไฟล์</th>
                <th width="100">ดาวน์โหลด</th>
                <th width="100">ลบ</th>
              </tr>
            </thead>
            <tbody>
            <?php
            $sql = 'SELECT * FROM stickers WHERE file_path IS NOT NULL';
			$q = mysql_query($sql);
			while($rs = mysql_fetch_assoc($q)){
			  if($rs['file_path'] != ""){
			?>
              <tr>
              	<td align="center"><input type="checkbox" name="selected[]" value="<?php echo $rs['id'];?>" /></td>
            	<td><?php echo $rs['id'];?></td>
                <td>
                	<a href="<?php echo $rs['file_path'];?>" title="ดาวน์โหลด">
                		<?php echo $rs['file_name'];?>
                    </a>					
              	</td>
                <td align="center">
                	<a href="<?php echo $rs['file_path'];?>" title="ดาวน์โหลด">
                		<img src="css/images/download.png" width="32"/>
                    </a>
              	</td>                
                <td align="center">
                	<a href="#" title="ลบ" id="delete_sticker">
                    	<img src="css/images/delete.png" width="20"/>
                    </a>
               	</td>
              </tr>
           	<?php 
			  }
			} 
			?>
            </tbody>
        </table>
	</div>
</div>
<script type="text/javascript">
$(function(){
	$('#products_list').dataTable({
		"bJQueryUI": true,
		"sPaginationType": "full_numbers"
	});
	$('#delete_now').click(function(){
		var selected="";
		var i = 0;
		$('#products_list tbody tr td').find('input[type=checkbox]:checked').each(function(){
			if($(this).val() != "on"){
				selected += "&selected["+i+"]="+$(this).val();
			}
			i++;
		});
		if(confirm("คุณมั่นใจหรือเปล่าว่าต้องการลบไฟล์นี้")){
		  if(selected != ""){
			$.ajax({
				type 	: 'GET',
				url		: 'query.php',
				data	: "delete_selected=true&table=stickers"+selected,
				dataType: 'text',
				success	: function(msg){
					location.href = '?m=stickers&p=upfile';
				}
			});
		  }else{
			alert('คุณยังไม่ได้เลือกอะไรเลย');  
		  }		
		}
	  return false;
	});
	$('a#delete_sticker').click(function(){
		var id = $(this).closest('tr').find('td:eq(1)').text();
		if(confirm("คุณมั่นใจหรือเปล่าว่าต้องการลบไฟล์นี้")){
			$.ajax({
				type 	: 'POST',
				url		: 'query.php',
				data	: {delete_sticker:true, id:id},
				dataType: 'text',
				success	: function(msg){
					location.href = '?m=stickers&p=upfile';
				}
			});
		}
		return false;
	});
});
</script>
